// pages/doing/doingTask.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
var currentIndex = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    doingdaitijiao: 0,
    doingdaishenhe: 0,
    doingyijujue: 0,
    doingyihuoshang: 0,
    doingshibai: 0,
    cellsOne: [],
    cellsTwo: [],
    cellsThree: [],
    cellsFour: [],
    cellsFive: [],
    page: 1,
    activeTab: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    currentIndex = options.index;
    this.setData({
      activeTab: parseInt(options.index)
    });
    this.requestList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1
    });
    this.requestList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    });
    this.requestList();
  },
//标签切换
onChange(event) {
  currentIndex = event.detail.index;
  this.setData({
    page: 1,
    activeTab: parseInt(event.detail.index)
  });
  this.requestList();
},
//放弃任务
fangqirenwuRequest(event) {
  console.log(event.currentTarget.dataset.orderno)
  wx.showModal({
    title: '提示',
    content: '是否要放弃当前任务',
    complete: (res) => {
      if (res.confirm) {
        let obj = {
          method: 'PUT',
          contentType: 'application/x-www-form-urlencoded',
          url: '/task-order/' + event.currentTarget.dataset.orderno + '/abandon',
          data: {
            orderNo: event.currentTarget.dataset.orderno
          }
        }
        httpUtils.request(obj).then(res => {
          this.onPullDownRefresh();
        }).catch(err => {
          console.log('ERROR')
        });
      }
    }
  });
},
//查看任务详情
taskDetailRequest(event) {
  wx.navigateTo({
    url: '/pages/task/taskDetail?taskNo='+event.currentTarget.dataset.taskno,
  })
},
//重新提交
taskSubmit(event) {
  wx.navigateTo({
    url: '/pages/task/taskSubmit?taskNo='+event.currentTarget.dataset.taskno,
  })
},
//任务进程
actionProgress(event) {
  wx.navigateTo({
    url: '/pages/doing/doingProgress?orderNo='+event.currentTarget.dataset.orderno,
  })
},
//请求列表
requestList() {
  var typeStr = "待提交";
  if (currentIndex == 1) {
    typeStr = "待审核";
  } else if (currentIndex == 2) {
    typeStr = "已拒绝";
  } else if (currentIndex == 3) {
    typeStr = "已获赏";
  } else if (currentIndex == 4) {
    typeStr = "任务失败";
  }
  let obj = {
    method: 'GET',
    url: '/task-order',
    showLoading: true,
    data: {
      page: this.data.page,
      size: 20,
      taskOrderStatus: typeStr
    }
  }
  httpUtils.request(obj).then(res => {
    if (currentIndex == 4) {
      if (this.data.page == 1) {
        this.setData({
          cellsFive: []
        });
      }
      const {
        cellsFive = []
      } = this.data;
      res.data.userTaskOrderListDtos.forEach(function (item, index) {
        cellsFive.push(item);
      });
      this.setData({
        cellsFive,
        doingshibai: res.data.count
      });
    } else if (currentIndex == 3) {
      if (this.data.page == 1) {
        this.setData({
          cellsFour: []
        });
      }
      const {
        cellsFour = []
      } = this.data;
      res.data.userTaskOrderListDtos.forEach(function (item, index) {
        cellsFour.push(item);
      });
      this.setData({
        cellsFour,
        doingyihuoshang: res.data.count
      });
    } else if (currentIndex == 2) {
      if (this.data.page == 1) {
        this.setData({
          cellsThree: []
        });
      }
      const {
        cellsThree = []
      } = this.data;
      res.data.userTaskOrderListDtos.forEach(function (item, index) {
        cellsThree.push(item);
      });
      this.setData({
        cellsThree,
        doingyijujue: res.data.count
      });
    } else if (currentIndex == 1) {
      if (this.data.page == 1) {
        this.setData({
          cellsTwo: []
        });
      }
      const {
        cellsTwo = []
      } = this.data;
      res.data.userTaskOrderListDtos.forEach(function (item, index) {
        cellsTwo.push(item);
      });
      this.setData({
        cellsTwo,
        doingdaishenhe: res.data.count
      });
    } else {
      if (this.data.page == 1) {
        this.setData({
          cellsOne: []
        });
      }
      const {
        cellsOne = []
      } = this.data;
      res.data.userTaskOrderListDtos.forEach(function (item, index) {
        cellsOne.push(item);
      });
      this.setData({
        cellsOne,
        doingdaitijiao: res.data.count
      });
    }
    
  }).catch(err => {
    console.log(err)
  });
}
})